#encoding=utf-8

import time,random
import socket,os
import multiprocessing
chars_pool='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
timehost=[
    '64.113.32.5',#nist.netservicesgroup.com
    '216.229.0.179',#nist1-lnk.binary.net
    '129.6.15.30',#time-c.nist.gov
    '128.138.141.172',#utcnist2.colorado.edu
    '216.228.192.69',#nist-time-server.eoni.com
    '24.56.178.140',#wwv.nist.gov
]
xor_tuple = [0x58, 0x92, 0x23, 0x17, 0x53, 0x29]

user_min_id=multiprocessing.Value('i',382668)
isrunning_local=multiprocessing.Value('i',0)

user_do_work=multiprocessing.Value('i',0)


def encode_b65(n):
    table = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.'
    result = []
    temp = n
    if 0 == temp:
        result.append('0')
    else:
        while 0 < temp:
            result.append(table[temp % 65])
            temp /= 65
    return ''.join([x for x in reversed(result)])
def decode_b65(str):
    table = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5,
             "6": 6, "7": 7, "8": 8, "9": 9,
             "a": 10, "b": 11, "c": 12, "d": 13, "e": 14, "f": 15, "g": 16,
             "h": 17, "i": 18, "j": 19, "k": 20, "l": 21, "m": 22, "n": 23,
             "o": 24, "p": 25, "q": 26, "r": 27, "s": 28, "t": 29, "u": 30,
             "v": 31, "w": 32, "x": 33, "y": 34, "z": 35,
             "A": 36, "B": 37, "C": 38, "D": 39, "E": 40, "F": 41, "G": 42,
             "H": 43, "I": 44, "J": 45, "K": 46, "L": 47, "M": 48, "N": 49,
             "O": 50, "P": 51, "Q": 52, "R": 53, "S": 54, "T": 55, "U": 56,
             "V": 57, "W": 58, "X": 59, "Y": 60, "Z": 61,
             "-": 62, "_": 63, ".":64}
    result = 0
    for i in xrange(len(str)):
        result *= 65
        result += table[str[i]]
    return result
def encode_b36(n):
    table = '0123456789abcdefghijklmnopqrstuvwxyz'
    result = []
    temp = n
    if 0 == temp:
        result.append('0')
    else:
        while 0 < temp:
            result.append(table[temp % 36])
            temp /= 36
    return ''.join(result)

def baseEnc(str):
    xStr=''
    for i in range(0,len(str)):
        xStr=xStr+chr(xor_tuple[i%6]^ord(str[i]))
    return xStr

def gettime_host(host):
    address = (host, 13)
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # s = socket.socket()
        s.connect(address)
        ra = s.recv(100)

        s.close()
        return  ra
    except:
        return ''

def gettime_internet():
    for i in range(0,len(timehost)):
        dtime=gettime_host(timehost[i])
        try:
            timelist=dtime.split(' ')
            strtime=timelist[1]+' '+timelist[2]
            return time.strptime(strtime,'%y-%m-%d %H:%M:%S')
        except:
            pass
    return time.gmtime()


def getgm_day():
    gmtime=gettime_internet()
    return gmtime.tm_year*10000+gmtime.tm_mon*100+gmtime.tm_mday
def getgm_day_str():
    return str(getgm_day())
def get_random_char():
    cindex=random.randint(0,len(chars_pool)-1)
    return chars_pool[cindex]
def getgm_last_h_str():
    xtime=getgmtime()
    dtime=int(xtime+360000)
    return str(dtime)
def getgmtime():
    return int(time.mktime(gettime_internet()))
def get_savepath():
    gmtime=gettime_internet()
    return str(gmtime.tm_year)+'-'+str(gmtime.tm_mon)+'-'+str(gmtime.tm_mday)+os.sep+str(gmtime.tm_hour)
def get_cur_time_str():
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
def get_cur_day_str():
    return time.strftime('%Y-%m-%d',time.localtime(time.time()))
def get_cur_hour_str():
    return time.strftime('%Y-%m-%d-%H',time.localtime(time.time()))
def checkislocal():
    spath=os.getcwd()
    if spath.find('skaly/')>0:
        isrunning_local.value=1
    pass

checkislocal()

if __name__=="__main__":
    checkislocal()
    print decode_b65('4109144')
    print encode_b36(1111123213)
    print encode_b36(11111213)
    print encode_b36(14553213)
    print encode_b36(17778978213)
    print encode_b36(111476523213)

    print baseEnc('11111')
    print baseEnc(baseEnc('11111'))
    xx=gettime_internet()
    yy=time.asctime(xx)
    print gettime_internet()
    print gettime_internet()
    print gettime_internet()
    print gettime_internet()
    print gettime_internet()

    print getgmtime()
    print getgm_day()
    print getgm_last_h_str()
    print get_random_char()
    print get_random_char()
